import time
from functools import wraps
from cryptography.fernet import Fernet
from fastapi.responses import HTMLResponse


__all__ = [
    "decrypted",
    "normal_color",
    "wait_after"
]

def decrypted(*args):
    cipher = Fernet(args[0])
    def wrapper(func):

        @wraps(func)
        async def inner(*args_0, **kwargs_0):
            encrypted_text = await func(*args_0, **kwargs_0)
            decrypted_text = cipher.decrypt(encrypted_text).decode('utf-8')
            return HTMLResponse(decrypted_text)
        return inner
    return wrapper


def normal_color(func):
    @wraps(func)
    def wrapper(text):
        src_text = func(text)
        dst_text = "INFO:     " + "\033[35m" + src_text + "\033[0m"
        print(dst_text)
        return None
    return wrapper

def wait_after(*args):
    set_time = args[0]
    def wrapper(func):
        @wraps(func)
        def inner():
            func()
            time.sleep(set_time)
            return None
        # 直接启动
        inner()
        return inner
    return wrapper